<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 刷新列表按钮 -->
    <el-button type="primary" class="refreshBtn" @click="refresh"
      >刷新</el-button
    >
    <!-- 卡片视图区 -->
    <el-card class="user-card" v-if="isShow">
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input
            v-model="queryInfo.order_no"
            clearable
            placeholder="请输入订单号"
          >
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="queryInfo.user_name"
            clearable
            placeholder="请输入用户名"
          >
          </el-input>
        </el-col>
        <el-col :span="3">
          <el-select v-model="queryInfo.status" placeholder="请选择订单状态">
            <el-option label="全部" value=""> </el-option>
            <el-option label="待支付" value="待支付"> </el-option>
            <el-option label="已支付" value="已支付"> </el-option>
            <el-option label="配送中" value="配送中"> </el-option>
            <el-option label="已完成" value="已完成"> </el-option>
          </el-select>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="queryInfo.sjr"
            clearable
            placeholder="请输入收货人"
          >
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="getOrderList">搜索</el-button>
        </el-col>
      </el-row>
      <!-- 列表区 -->
      <el-table
        v-loading="loading"
        :data="orderlist"
        @expand-change="expandChange"
        stripe
        border
        height="75vh"
        class="table"
        :highlight-current-row="true"
      >
        <!-- 下拉 -->
        <el-table-column type="expand" :highlight-current-row="true">
          <template slot-scope="props">
            <el-card class="el-Card">
              <h2>订单详情</h2>
              <!-- 订单状态 -->
              <el-steps
                simple
                :active="getActive(props.row.status)"
                space="300px"
              >
                <el-step title="待支付"></el-step>
                <el-step title="已支付"></el-step>
                <el-step title="配送中"></el-step>
                <el-step title="已完成"></el-step>
              </el-steps>
              <!-- 订单详情 -->
              <el-table :data="props.row.data" stripe border class="table">
                <el-table-column type="index" label="#"></el-table-column>
                <el-table-column
                  prop="goods_name"
                  label="商品名称"
                  width="700"
                ></el-table-column>
                <el-table-column label="商品图片（点击预览）">
                  <template slot-scope="props">
                    <el-image
                      style="width: 50px; height: 50px"
                      :src="props.row.goods_big_logo"
                      :preview-src-list="[props.row.goods_big_logo]"
                    ></el-image>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="goods_id"
                  label="商品id"
                ></el-table-column>
                <el-table-column
                  prop="goods_price"
                  label="商品价格"
                ></el-table-column>
                <el-table-column
                  prop="cart_num"
                  label="商品数量"
                ></el-table-column>
                <el-table-column prop="total" label="总价"></el-table-column>
              </el-table>
            </el-card>
          </template>
        </el-table-column>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="order_no" label="订单编号"></el-table-column>
        <el-table-column prop="user_name" label="用户名"></el-table-column>
        <el-table-column prop="sjr" label="收件人"></el-table-column>
        <el-table-column
          prop="sjdz"
          label="收件地址"
          width="400"
        ></el-table-column>
        <el-table-column prop="tel" label="电话"></el-table-column>
        <el-table-column
          prop="total_money"
          label="总金额（元）"
        ></el-table-column>
        <el-table-column prop="status" label="订单状态"> </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[1, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      inputText: "",
      // 请求的参数
      queryInfo: {
        order_no: "",
        user_name: "",
        status: "",
        sjr: "",
        page: 1, //页数
        pagesize: 10, //每页条数
      },
      // 订单列表
      orderlist: [],
      // 订单总数
      total: 0,
      // 加载中的显示隐藏
      loading: false,
      //
      isShow: true,
      // 单条商品信息
      listInfo: [],
      // 当前状态
      active: 1,
    };
  },
  created() {
    this.getOrderList();
  },
  methods: {
    // 获取所有订单信息
    async getOrderList() {
      this.loading = true;
      const { data: res } = await this.$http.get("/api/orders", {
        params: this.queryInfo,
        ...this.getToken,
      });
      console.log(res);
      if (res.code !== 1) return this.$message.error("获取商品订单失败！");
      this.loading = false;
      this.orderlist = res.data.list;
      this.total = res.data.total;
      // console.log(this.orderlist);
    },
    // 获取单个订单信息
    async getOrderById(id) {
      const { data: res } = await this.$http.get(
        "/api/orders/od/" + id,
        this.getToken
      );
      if (res.code !== 1) return this, $message.error("获取详细信息失败！");
      this.listInfo = res.data;
      console.log("res", res);
    },
    // 下拉的展开与收起
    expandChange(row, expandedRows) {
      console.log(row, expandedRows);
      if (expandedRows.length === 0) return;
      const orderId = row.order_id;
      this.getOrderById(orderId);
    },
    // 刷新
    refresh() {
      this.isShow = false;
      this.$nextTick(() => setTimeout(() => (this.isShow = true), 50));
    },
    // 监听条数改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getOrderList();
    },
    // 监听页数改变
    handleCurrentChange(newPag) {
      this.queryInfo.page = newPag;
      this.getOrderList();
    },
    // 获得订单状态
    getActive(status) {
      // console.log(status);
      if (status == "待支付") return 1;
      else if (status == "已支付") return 2;
      else if (status == "配送中") return 3;
      else return 4;
    },
  },
  computed: {
    getToken() {
      return { headers: { authorization: sessionStorage.getItem("token") } };
    },
  },
};
</script>

<style lang="less" scoped>
.orderStatus {
  margin-top: 20px;
  margin-left: 20px;
}
.el-steps {
  margin: 20px auto;
}
</style>